package com.ce.pms.house.web;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.model.BankAccountDomain;
import com.ce.pms.charge.service.IBankAccountService;
import com.ce.pms.charge.service.IFeeBankService;
import com.ce.pms.charge.vo.BankAccountVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.ICoalitionHouseService;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.vo.CoalitionHouseVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;

/***
 * 合并房管理
 * 逻辑、功能相关描述:
 *
 * @author 燕大为 编写日期：2008-7-3
 */
public class CoalitionHouseAction extends PmsBaseAction {

	private ICoalitionHouseService coalitionHouseService;
	private IBankAccountService bankAccountService;
	private IHouseService houseService;
	private IFeeBankService feeBankService;



	public void setFeeBankService(IFeeBankService feeBankService) {
		this.feeBankService = feeBankService;
	}


	public void setCoalitionHouseService(
			ICoalitionHouseService coalitionHouseService) {
		this.coalitionHouseService = coalitionHouseService;
	}


	public void setBankAccountService(IBankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}


	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}


	/***
	 * Function: 初始化新增合并房
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-4
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initCoalitionHouseAdd(@Read(key="projectId")@Out(key="projectId") Long projectId)
	{
		setAttribute("bankList", feeBankService.getAllBankList());//获取所有银行名称
		return "initColitionHouseAdd";
	}


	/***
	 * Function:新增合并房
	 * @param coalitionHouse
	 * @param bankAccount
	 * Create author:燕大为
	 * Create on:2008-7-7
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void addCoalitionHouse(
			@Read CoalitionHouseVO coalitionHouse,
			@Read BankAccountVO bankAccount)
	{
		validationHouseInSameBuilding(coalitionHouse.getChoiceHouseStr(),coalitionHouse.getCoalitionType());
		coalitionHouse.setOperator(getUserId());
		if(BlankUtil.isBlank(bankAccount.getBankId()))
			bankAccount = null;
		coalitionHouse.setBankAccount(bankAccount);
		coalitionHouseService.addCoalitionHouse(coalitionHouse);
		this.renderText("true");
	}



	/***
	 * Function: 初始化修改合并房界面
	 * @param coalitionHouseId
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-7
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initCoalitionHouseEdit(@Read(key = "coalitionHouseId") Long coalitionHouseId)
	{

		CoalitionHouseVO coalitionHouse = coalitionHouseService.findCoalitionHouseById(coalitionHouseId);
		StringBuffer strValue = new StringBuffer();//组成管理房的产权房字符串如1-2A,1-2B
		StringBuffer strKey = new StringBuffer();//30,31
		if(!BlankUtil.isBlank(coalitionHouse))
		{
			Map<Long, String> houseMap = coalitionHouse.getHouseMap();
			Set set = houseMap.entrySet();
			for(Iterator iter = set.iterator() ; iter.hasNext() ;)
			{
				Map.Entry entry = (Entry) iter.next();
				strValue.append(entry.getValue()).append(",");
				strKey.append(entry.getKey()).append(",");
			}
			strValue = strValue.deleteCharAt(strValue.length()-1);
			strKey = strKey.deleteCharAt(strKey.length()-1);
		}
		coalitionHouse.setChoiceHouseStr(strKey.toString());
		setAttribute("bankList", feeBankService.getAllBankList());//获取所有银行名称
		setAttribute("coalitionHouseDisplay", strValue);
		setAttribute("coalitionHouse", coalitionHouse);
		setAttribute("bankAccount",coalitionHouse.getBankAccount());
		return "initCoalitionHouseEdit";
	}


	/***
	 * Function: 修改合并管理房
	 * @param coalitionHouse
	 * @param bankAccount
	 * Create author:燕大为
	 * Create on:2008-7-9
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void editCoalitionHouse(
			@Read CoalitionHouseVO coalitionHouse,
			@Read BankAccountVO bankAccount)
	{
		validationHouseInSameBuilding(coalitionHouse.getChoiceHouseStr(),coalitionHouse.getCoalitionType());
		coalitionHouse.setOperator(getUserId());
		if(BlankUtil.isBlank(bankAccount.getBankId()))
			bankAccount = null;
		coalitionHouse.setBankAccount(bankAccount);
		coalitionHouseService.editCoalitionHouse(coalitionHouse);
		this.renderText("true");
	}



	/***
	 * Function: 初始化删除操作
	 * @param coalitionHouseId 管理房ID
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-7
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initCoalitionHouseDelete(@Read(key = "coalitionHouseId")Long coalitionHouseId)
	{
		CoalitionHouseVO coalitionHouse =coalitionHouseService.findCoalitionHouseById(coalitionHouseId);
		StringBuffer strValue = new StringBuffer();
		if(!BlankUtil.isBlank(coalitionHouse))
		{
			Map<Long, String> houseMap = coalitionHouse.getHouseMap();
			Set set = houseMap.entrySet();
			for(Iterator iter = set.iterator() ; iter.hasNext() ;)
			{
				Map.Entry entry = (Entry) iter.next();
				strValue.append(entry.getValue()).append(",");
			}
			strValue = strValue.deleteCharAt(strValue.length()-1);
		}
		setAttribute("message","您确定要解除"+strValue+"的合并吗?");
		setAttribute("coalitionId", coalitionHouseId);
		return "initCoalitionHouseDelete";
	}



	/***
	 * Function: 解除合并管理房
	 * @param CoalitionHouseId 管理房ID
	 * Create author:燕大为
	 * Create on:2008-7-8
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void unchainCoalition(
			@Read(key = "coalitionHouseId") Long CoalitionHouseId)
	{
		Long modifyer = getUserId();
		coalitionHouseService.deleteCoalitionHouse(CoalitionHouseId, modifyer);
		this.renderText("");
	}


	/***
	 * 功能描述:  选择银行帐号界面<br>
	 * @param houseNumberStr 以选择的房号串(1-2A,1-2B)
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-7-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Deprecated
	public String initChoiceBankAccount(@Read(key = "houseIdStr") String houseIdStr)
	{
		List<BankAccountVO> list = new ArrayList<BankAccountVO>();
		if(!BlankUtil.isBlank(houseIdStr))
		{
			if(houseIdStr.endsWith(","))
			   houseIdStr = houseIdStr.substring(0, houseIdStr.length()-1);
		}
		List<BankAccountDomain> bankAccountDomainList = bankAccountService.currentBankAccount(houseIdStr,false);
		if(!BlankUtil.isBlank(bankAccountDomainList)){
			for(BankAccountDomain bankAccount : bankAccountDomainList)
			{
				list.add(BankAccoutDO2VO(bankAccount));
			}
		}
		setAttribute("bankAccountList", list);
		setAttribute("totalRows", new Integer(list.size()));
		return "choiceBankAccount";
	}


	/**
	 * 功能描述: 验证所选择的房间是否在同一楼栋下面 <br>
	 * @param houseIdStr
	 * @param coalitionType
	 * Create author:yandawei <br>
	 * Create on:2008-8-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void validationHouseInSameBuilding(String houseIdStr,Long coalitionType)
	{
		SysrefcodeDO sys =  sysrefcodeService.findSysrefcodeById(coalitionType);
		if(SysrefcodeDO.DOUBLE_COALITIONHOUSE.equals(sys.getValue())||
				SysrefcodeDO.COMMON_DOUBLE_COALITIONHOUSE.equals(sys.getValue()))
		{
			if(houseIdStr.endsWith(","))
				houseIdStr = houseIdStr.substring(0, houseIdStr.length()-1);
			if(!houseService.checkHouseWhetherWithinSameBuilding(houseIdStr)){
				throw new BusinessException("house:house_coalition_notinsamebuilding",BusinessException.FAILURE);
			}
		}
	}


	/**
	 * 功能描述:  将银行帐户DO转为VO<br>
	 * @param bankAccount
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-7-31  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private BankAccountVO BankAccoutDO2VO(BankAccountDomain bankAccount)
	{
		BankAccountVO bankAccountVO = new BankAccountVO();
		bankAccountVO.setBankId(bankAccount.getKeyword());
		bankAccountVO.setAccNumber(bankAccount.getAccNumber());
		bankAccountVO.setBankName(bankAccount.getBank().getBankName());
		bankAccountVO.setContactNumber(bankAccount.getContactNumber());
		bankAccountVO.setHouseNumber(bankAccount.getHouse().getHouseNumber());
		bankAccountVO.setAccOwner(bankAccount.getAccOwner());
		return bankAccountVO;
	}
}
